/**
 * @copyright Copyright (c) 2025 
 * CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author xxxcsg
 * @brief devinfo工具，获取设备信息
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
#include "hilog/log.h"
#include "parameter.h"
#include "common.h"


#define DEV_INFO_ID                 "const.product.ID"
#define DEV_INFO_NAME               "const.product.ohos.fullname"
#define DEV_INFO_VERSION            "const.product.software.version"
#define DEV_INFO_VERSION_ID         "const.product.firstapiversion"
#define DEV_INFO_PRETTY_NAME        "const.product.prettyname"
#define DEV_INFO_DISTRO_CODE_NAME   "const.product.distro.codename"
#define DEV_INFO_BOARD              "const.product.hardwareversion"
#define DEV_INFO_FIRMWARE_VERSION   "const.product.firmware.version"
#define DEV_INFO_IPC_VERSION        "const.product.ipc.version"
#define DEV_INFO_ESN                "const.product.esn"
#define DEV_INFO_DEVICE_TYPE        "const.product.device.type"
#define DEV_INFO_MAC                "const.product.mac"
#define DEV_INFO_CHIP_ID            "const.product.chip.id"

#undef LOG_DOMAIN
#undef LOG_TAG
#define LOG_DOMAIN 0xD006666  // 标识业务领域，范围0xD000000~0xD0FFFFF
#define LOG_TAG "HawkEye"



#define HAWK_EYE_HILOG_TAG LOG_TAG


double get_cpu_temperture()
{
    FILE *fp;
	char cmd[128] = {0};
	char line[1024] = {0};
    int len = 0;
    int tmpNum = 0;
    double temperature = 0.0;

    sprintf(cmd,"cat /sys/class/thermal/thermal_zone0/temp");

	// fp = popen("bspmd.l 0x1102a008 | grep 0000:", "r");
    fp = popen(cmd, "r");
	if (fp == NULL)
	{
        // printf("\033[31mpopen cmd error!\033[39m\n");
        HILOG_ERROR(LOG_CORE, "open cmd error!");
        return -1;
    }

	if (fgets(line, sizeof(line), fp) == NULL) 
	{
		pclose(fp);
		// printf("\033[31mget temperature error!\033[39m\n");
        return -1;
	}

	pclose(fp);

    len = strlen(line);
    if(len == 0)
    {
        // printf("\033[31mget empty data!\033[39m\n");
        return -1;
    }
    
    tmpNum = atoi(line);
    temperature = tmpNum / 1000.0;
    // printf("%.2lf\n", temperature);
    HILOG_INFO(LOG_CORE, "temperature:%.2lf\n", temperature);

    return temperature;
}

int get_cpu_freq()
{
    FILE *fp;
	char cmd[128] = {0};
	char line[1024] = {0};
    int len = 0;
    int tmpNum = 0;
    int freq = 0;

    sprintf(cmd,"cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq");
    fp = popen(cmd, "r");
	if (fp == NULL)
	{
        // printf("\033[31mpopen cmd error!\033[39m\n");
        return -1;
    }

	if (fgets(line, sizeof(line), fp) == NULL) 
	{
		pclose(fp);
		// printf("\033[31mget freq error!\033[39m\n");
        return -1;
	}

	pclose(fp);

    len = strlen(line);
    if(len == 0)
    {
        // printf("\033[31mget empty data!\033[39m\n");
        return -1;
    }

    tmpNum = atoi(line);
    freq = tmpNum / 1000;

    // printf("freq:%d\n", freq);
    HILOG_INFO(LOG_CORE, "freq:%d\n", freq);

    return freq;
}

void filterchar(char *str)
{
    int len = strlen(str);
    int i = 0;
    while(i < len) {
        if(str[i] == '\n' || str[i] == '\r') {
            // printf("%c", str[i]);
            str[i] = '\0';
            break;
        }
        i++;
    }
    return;
}

#if 0
int get_dev_param(char* inStr,char* outStr)
{
    char defBuf[32] = {0};
    int ret = GetParameter(inStr,defBuf,outStr,sizeof(outStr));
    if(ret <= 0)
    {
        HILOG_ERROR(LOG_CORE, "open cmd error!");
        printf("open cmd error! ret = %d\n",ret);
        return 1;
    }
    printf("in-str = %s, ret = %d, def-buf = %s, ret-buf = %s\n",inStr,ret, defBuf, outStr);

    // filterchar(outStr);
    // printf("outStr:%s\n",outStr);

    return 0;
}
#else  
int get_dev_param(char* inStr,char* outStr)
{
    FILE *fp;
	char cmd[128] = {0};
	char line[512] = {0};
    char *returnStr = NULL;
    int len = 0;

    sprintf(cmd,"begetctl param get |grep %s",inStr);
	fp = popen(cmd, "r");
	if (fp == NULL)
	{
        // printf("\033[31mpopen cmd error!\033[39m\n");
        return -1;
    }

	if (fgets(line, sizeof(line), fp) == NULL) 
	{
		pclose(fp);
		// printf("\033[31mget freq error!\033[39m\n");
        return -1;
	}

	pclose(fp);

    len = strlen(line);
    if(len == 0)
    {
        // printf("\033[31mget empty data!\033[39m\n");
        return -1;
    }

    returnStr = strstr(line,"=");
    if(returnStr == NULL)
    {
        // printf("\033[31mget data error!\033[39m\n");
        return -1;   
    }

    strcpy(outStr,returnStr + 2);
    filterchar(outStr);
    // printf("outStr:%s\n",outStr);

    return 0;
}
#endif
void get_devinfo(void)
{
    char id[64] = {0};
    // char* osName = NULL;
    // char* osVersion = NULL;
    char versionId[64] = {0};
    char prettyName[64] = {0};
    char distroCodeName[64] = {0};
    char board[64] = {0};
    char firmwareVersion[64] = {0};
    char ipcVersion[64] = {0};
    char esn[64] = {0};
    char deviceType[64] = {0};
    char mac[64] = {0};
    char chipId[64] = {0};

    char *jsonString = NULL;
    cJSON *root = NULL;

    const char* osName = GetOSFullName();
    const char* osVersion = GetDisplayVersion();
    
    if(get_dev_param(DEV_INFO_ID,id)!=0)
    {
        strcpy(id,"NULL");
    }
    // if(get_dev_param(DEV_INFO_NAME,name)!=0)
    // {
    //     strcpy(name,"NULL");
    // }
    // if(get_dev_param(DEV_INFO_VERSION,version)!=0)
    // {
    //     strcpy(version,"NULL");
    // }
    if(get_dev_param(DEV_INFO_VERSION_ID,versionId)!=0)
    {
        strcpy(versionId,"NULL");
    }
    if(get_dev_param(DEV_INFO_PRETTY_NAME,prettyName)!=0)
    {
        strcpy(prettyName,"NULL");
    }
    if(get_dev_param(DEV_INFO_DISTRO_CODE_NAME,distroCodeName)!=0)
    {
        strcpy(distroCodeName,"NULL");
    }
    if(get_dev_param(DEV_INFO_BOARD,board)!=0)
    {
        strcpy(board,"NULL");
    }
    if(get_dev_param(DEV_INFO_FIRMWARE_VERSION,firmwareVersion)!=0)
    {
        strcpy(firmwareVersion,"NULL");
    }
    if(get_dev_param(DEV_INFO_IPC_VERSION,ipcVersion)!=0)
    {
        strcpy(ipcVersion,"NULL");
    }
    if(get_dev_param(DEV_INFO_ESN,esn)!=0)
    {
        strcpy(esn,"NULL");
    }
    if(get_dev_param(DEV_INFO_DEVICE_TYPE,deviceType)!=0)
    {
        strcpy(deviceType,"NULL");
    }
    if(get_dev_param(DEV_INFO_MAC,mac)!=0)
    {
        strcpy(mac,"NULL");
    }
    if(get_dev_param(DEV_INFO_CHIP_ID,chipId)!=0)
    {
        strcpy(chipId,"NULL");
    }

    root = cJSON_CreateObject();
    if(!root)
    {
        // printf("\033[31mjson root create error!\033[39m\n");
        return;
    }

    if(!cJSON_AddStringToObject(root,"ID", id))
    {
        // printf("\033[31mjson jsonId create error!\033[39m\n");
        goto exit;
    }

    if(osName == NULL)
    {
        if(!cJSON_AddStringToObject(root,"NAME", "NULL"))
        {
            printf("\033[31mjson jsonName create error!\033[39m\n");
            goto exit;
        }
    }
    else
    {
        if(!cJSON_AddStringToObject(root,"NAME", osName))
        {
            printf("\033[31mjson jsonName create error!\033[39m\n");
            goto exit;
        }
    }
    if(osVersion == NULL)
    {
        if(!cJSON_AddStringToObject(root,"VERSION", "NULL"))
        {
            printf("\033[31mjson jsonName create error!\033[39m\n");
            goto exit;
        }
    }
    else
    {
        if(!cJSON_AddStringToObject(root,"VERSION", osVersion))
        {
            printf("\033[31mjson jsonName create error!\033[39m\n");
            goto exit;
        }
    }

    if(!cJSON_AddStringToObject(root,"VERSION_ID", versionId))
    {
        // printf("\033[31mjson jsonVersionId create error!\033[39m\n");
        goto exit;
    }

    if(!cJSON_AddStringToObject(root,"PRETTY_NAME", prettyName))
    {
        // printf("\033[31mjson jsonPrettyName create error!\033[39m\n");
        goto exit;
    }

    if(!cJSON_AddStringToObject(root,"DISTRO_CODENAME", distroCodeName))
    {
        // printf("\033[31mjson jsonDistroCodeName create error!\033[39m\n");
        goto exit;
    }

    if(!cJSON_AddStringToObject(root,"BOARD", board))
    {
        // printf("\033[31mjson jsonBoard create error!\033[39m\n");
        goto exit;
    }

    if(!cJSON_AddStringToObject(root,"firmware_version", firmwareVersion))
    {
        // printf("\033[31mjson jsonFirmwareVersion create error!\033[39m\n");
        goto exit;
    }

    if(!cJSON_AddStringToObject(root,"ipc_version", ipcVersion))
    {
        // printf("\033[31mjson jsonIpcVersion create error!\033[39m\n");
        goto exit;
    }
    if(!cJSON_AddStringToObject(root,"ESN", esn))
    {
        // printf("\033[31mjson esn create error!\033[39m\n");
        goto exit;
    }
    if(!cJSON_AddStringToObject(root,"device_type", deviceType))
    {
        // printf("\033[31mjson deviceType create error!\033[39m\n");
        goto exit;
    }
    if(!cJSON_AddStringToObject(root,"MAC", mac))
    {
        // printf("\033[31mjson mac create error!\033[39m\n");
        goto exit;
    }
    if(!cJSON_AddStringToObject(root,"CHIP_ID", chipId))
    {
        // printf("\033[31mjson chipId create error!\033[39m\n");
        goto exit;
    }

    jsonString = cJSON_Print(root);
    if(!jsonString)
    {
        // printf("\033[31mjson jsonString create error!\033[39m\n");
        goto exit;
    }

    printf("%s\n", jsonString);
    free(jsonString);

exit:
    cJSON_Delete(root);
    return;
}

void get_cpu_info(void)
{
    cJSON *root = NULL;

    double cpuTemp = 0.0;
    int cpuFreq = 0;
    cpuTemp = get_cpu_temperture();
    cpuFreq = get_cpu_freq();


    root = cJSON_CreateObject();
    if(!cJSON_AddNumberToObject(root, "cpuTemp", cpuTemp))
    {
        // printf("\033[31mjson cpuTemp create error!\033[39m\n");
        cJSON_Delete(root);
        return;
    }
    if(!cJSON_AddNumberToObject(root, "cpuFrequency", cpuFreq))
    {
        // printf("\033[31mjson cpuFrequency create error!\033[39m\n");
        cJSON_Delete(root);
        return;
    }

    char *jsonString = cJSON_Print(root);
 
    printf("%s\n", jsonString);
 
    cJSON_Delete(root);
    free(jsonString);

}

void print_help(void)
{
    printf("-h        help\n");
    printf("-a        get dev info\n");
    printf("-c        get cpu info\n");
    printf("-v        show tool version\n");
}

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        print_help();
        return 0;
    }

    if(!strcmp(argv[1],"-a"))
    {
        get_devinfo();
    }
    else if(!strcmp(argv[1],"-c"))
    {
        get_cpu_info();
    }
    else if(!strcmp(argv[1],"-v"))
    {
        printf_func_version(HAWK_EYE_FUNC_DEVINFO);
    }
    else
    {
        print_help();
    }

    return 0;
}